package com.strelitzia.admin.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.strelitzia.admin.Entity.AdminEntity;
import com.strelitzia.admin.constant.AdminConstant;
import com.strelitzia.admin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

/**
 * @create 2024/1/21 11:52
 */

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    private String login(String username, String password, HttpSession session, Model model) {
        AdminEntity one = adminService.getOne(new QueryWrapper<AdminEntity>().eq("username", username).eq("password", password));
        if(one != null) {
            session.setAttribute(AdminConstant.SessionId, JSON.toJSONString(one));
            return "redirect:/";
        }else {
            model.addAttribute("message", "用户名或密码错误");
            model.addAttribute("username", username);
            model.addAttribute("password", password);
            return "login";
        }
    }
}
